//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * buycarAction
 * 项目名称:代购系统
 * @author Mengxiangliang
 * 功能描述:
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * @version [V1] 
 */
package com.gamutsoft.amb.buycar.action;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.base.action.BaseAction;
import com.gamutsoft.amb.buycar.model.Buycar;
import com.gamutsoft.amb.buycar.service.IBuycarService;
import com.gamutsoft.amb.commodity.model.Commodity;
import com.gamutsoft.amb.commodity.service.ICommodityService;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.user.model.SystemUser;
import com.html.MainHTMLPojo;
import com.html.pojo.CommodityPojo;


@SuppressWarnings({"serial","rawtypes"})
public class BuycarAction extends BaseAction {
	
	private Buycar buycar;
	
	private List<Map> buycarList;
	
	
	private IBuycarService buycarService;
	
	private ICommodityService commodityService;
	
	private Commodity commodity;
	
	private String parseUrl;
	
	private List<Map> commodityList;
	
	private Map<String,List<Map>> buyCommodityMap;

	/**
	 * 根据url解析商品信息
	 */
	public String parseGoodInfo()throws BusinessException, Exception{
		try{
			if(parseUrl!=null && !"".equals(parseUrl)){
//			  MainHTMLPojo mainHTMLPojo=new MainHTMLPojo();
//			  commodity=new Commodity();
//			  CommodityPojo cp=mainHTMLPojo.getDiffPojoByUrl(parseUrl);
//			  if(cp!=null){
//				    String price=(cp.getPrice()==null?"0.00":cp.getPrice()).equals("")?"0.00":(cp.getPrice()==null?"0.00":cp.getPrice());
//				    String freight=(cp.getFreight()==null?"0.00":cp.getFreight()).equals("")?"0.00":(cp.getFreight()==null?"0.00":cp.getFreight());
//				    commodity.setPrice(new BigDecimal(price));
//					commodity.setBuycount(1L);
//					commodity.setImgurl(cp.getImgUrl());
//					commodity.setDomainname(cp.getDomainname());
//					commodity.setUrl(cp.getDetailInfoUrl());
//					commodity.setShopname(cp.getShop());
//					commodity.setName(cp.getName());
//					commodity.setFreight(new BigDecimal(freight));
//			  }
				//测试
				commodity=new Commodity();
			    String price="100.00";
			    String freight="500.00";
			    commodity.setPrice(new BigDecimal(price));
				commodity.setBuycount(1L);
				commodity.setImgurl("http://www.taobao.com");
				commodity.setDomainname("测试商品");
				commodity.setUrl(parseUrl);
				commodity.setShopname("淘宝卖家:6008");
				commodity.setName("test");
				commodity.setFreight(new BigDecimal(freight));
			}
		}catch(Exception e){
			log.error(e,e);
		}
		return "show_good_info";
	}
	/**
	 * 创建购物车并添加商品
	 */
	public String  addBuyCar()throws BusinessException, Exception{
		if(commodity!=null ){
			if(commodity.getId()==null || "".equals(commodity.getId())){
			 buycarService.createBuyCarByGood(commodity, super.getSystemUser());
			 return "add_good_success";
			}else{
				Commodity domain=commodityService.searchCommodityById(commodity.getId());
				if(domain!=null){
					domain.setImgurl(commodity.getImgurl());
					domain.setUrl(commodity.getUrl());
                    domain.setName(commodity.getName());
                    domain.setShopname(commodity.getShopname());
                    domain.setPrice(commodity.getPrice());
                    domain.setBuycount(commodity.getBuycount());
                    domain.setRemark(commodity.getRemark());
                    commodityService.updateCommodity(domain);
				}
				return "view_buyCard_success";
			}
		}
		return "add_good_success";
	}
	/**
	 * 跳转到继续购物的页面
	 */
	public String continueBuyGood()throws BusinessException, Exception{
		return "show_good_info";
	}
	/**
	 * ajax 解析url
	 */
	public void ajaxParseUrl()throws BusinessException, Exception{
		PrintWriter out;
		out = this.getResponse().getWriter();
		String result="";
		try{
			if(parseUrl!=null && !"".equals(parseUrl)){
			  MainHTMLPojo mainHTMLPojo=new MainHTMLPojo();
			  commodity=new Commodity();
			  CommodityPojo cp=mainHTMLPojo.getDiffPojoByUrl(parseUrl);
			  if(cp!=null){
			    String price=(cp.getPrice()==null?"0.00":cp.getPrice()).equals("")?"0.00":(cp.getPrice()==null?"0.00":cp.getPrice());
			    String freight=(cp.getFreight()==null?"0.00":cp.getFreight()).equals("")?"0.00":(cp.getFreight()==null?"0.00":cp.getFreight());
			    //生成json格式
				result="{'price':'"+price+"','buyCount':'"+1+"','imgurl':'"+cp.getImgUrl()+"','domainname':'"+cp.getDomainname()+"',"+
				"'url':'"+cp.getDetailInfoUrl()+"','name':'"+cp.getName()+"','freight':'"+freight+"'";
			  }
			}
		}catch(Exception e){
			log.error(e,e);
		}
		out.write(result);
	}
	/**
	 * 获取购物车数量
	 */
	public void ajaxForBuyCarNum()throws BusinessException, Exception{
		PrintWriter out;
		out = this.getResponse().getWriter();
		String result="";
		SystemUser user=super.getSystemUser();
		if(user!=null){
			Long userId=user.getId();
			QueryExample qe=new QueryExample();
			qe.addCondictions("T.USER_ID","=",userId);
			qe.addCondictions("T.STATUS","=",Constants.STATE_VALID);
			buycarList=buycarService.searchBuycarList(qe);
			if(buycarList!=null){
				result=buycarList.size()+"";
			}else{
				result="0";
			}
		}
		out.write(result);
	}
	/**
	 * 查看购物车商品
	 */
	public String gotoBuyCarGoods()throws BusinessException, Exception{
		Buycar domain=new Buycar();
		domain.setUserId(super.getSystemUser().getId());
		domain.setStatus(Constants.STATE_VALID);
		List<Map> list=buycarService.searchGoodByUserId(domain);
		coverMap(list);
		return  "buyCarList";
	}
	
	/**
	 * 购物车物品分类
	 */
	private void coverMap(List<Map>list){
		buyCommodityMap =new HashMap();
		List<Map> otherMap=new ArrayList();
		buyCommodityMap.put("other", otherMap);
		
		try{
			if(list!=null && list.size()!=0){
				for(Map map:list){
					String shopName=map.get("shopname")==null?"":map.get("shopname").toString();
					double price= Double.parseDouble(map.get("price")==null?"0": map.get("price").toString());
					Double count = Double.parseDouble(map.get("buycount")==null?"1": map.get("buycount").toString());
					Double freight = Double.parseDouble(map.get("freight")==null?"1": map.get("freight").toString());
					//设置amount
					map.put("amount", price*count+freight);
					if("".equals(shopName)){
						otherMap.add(map);
					}else{
						if(buyCommodityMap.containsKey(shopName)){
							buyCommodityMap.get(shopName).add(map);
						}else{
							List<Map> childList=new ArrayList();
							childList.add(map);
							buyCommodityMap.put(shopName, childList);
						}
					}
				}
			}
		}catch(Exception e){
			log.error(e,e);
		}
	}
	/**
	 * 删除购物车商品
	 */
	public String deleteCommodity(){
		try{
			if(commodity!=null && commodity.getId()!=null &&!"".equals(commodity.getId())){
				commodityService.deleteCommodityById(commodity.getId());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "view_buyCard_success";
	}
	/**
	 * 更新商品信息页面
	 */
	public String initUpdateCommodity(){
		try{
			if(commodity!=null && commodity.getId()!=null &&!"".equals(commodity.getId())){
				commodity=commodityService.searchCommodityById(commodity.getId());
			}
		}catch(Exception e){
			log.error(e,e);
		}
		return "update_show_good_info";
	}
	/**
	 * 更新商品
	 */
	public String updateCommodity(){
		try{
			if(commodity!=null && commodity.getId()!=null &&!"".equals(commodity.getId())){
				Commodity domain =commodityService.searchCommodityById(commodity.getId());
				domain.setBuycount(commodity.getBuycount());
				domain.setFreight(commodity.getFreight());
				domain.setImgurl(commodity.getImgurl());
				domain.setDomainname(commodity.getDomainname());
				domain.setName(commodity.getName());
				domain.setPrice(commodity.getPrice());
				domain.setRemark(commodity.getRemark());
				domain.setShopname(commodity.getShopname());
				commodityService.updateCommodity(domain);
			}
			
		}catch(Exception e){
			log.error(e,e);
		}
		return "view_buyCard_success";
	}
	/**
	 * @return the buycar
	 */
	public Buycar getBuycar() {
		return buycar;
	}

	/**
	 * @param buycar the buycar to set
	 */
	public void setBuycar(Buycar buycar) {
		this.buycar = buycar;
	}

	/**
	 * @return the buycarList
	 */
	public List<Map> getBuycarList() {
		return buycarList;
	}

	/**
	 * @param buycarList the buycarList to set
	 */
	public void setBuycarList(List<Map> buycarList) {
		this.buycarList = buycarList;
	}

	/**
	 * @param buycarService the buycarService to set
	 */
	public void setBuycarService(IBuycarService buycarService) {
		this.buycarService = buycarService;
	}

	public Commodity getCommodity() {
		return commodity;
	}

	public void setCommodity(Commodity commodity) {
		this.commodity = commodity;
	}

	public String getParseUrl() {
		return parseUrl;
	}

	public void setParseUrl(String parseUrl) {
		this.parseUrl = parseUrl;
	}
	public void setCommodityService(ICommodityService commodityService) {
		this.commodityService = commodityService;
	}
	public List<Map> getCommodityList() {
		return commodityList;
	}
	public void setCommodityList(List<Map> commodityList) {
		this.commodityList = commodityList;
	}
	public Map<String, List<Map>> getBuyCommodityMap() {
		return buyCommodityMap;
	}
	public void setBuyCommodityMap(Map<String, List<Map>> buyCommodityMap) {
		this.buyCommodityMap = buyCommodityMap;
	}

	
}
